package com.example.module_service.vm

import com.example.lib_common.view.vm.BaseViewModel
import com.example.module_service.api.ServiceApiService
import com.example.module_service.entity.MemberEntity
import com.example.module_service.model.ServiceRepository
import com.example.module_service.model.ServiceResultData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MemberViewModel @Inject constructor(private val apiService: ServiceApiService):BaseViewModel<ServiceRepository,ServiceResultData<MemberEntity>>(){
    fun getMember(userId:Int){
        mViewModelScope.launch(mDispatcher){
            try {
                val member = apiService.getMember(userId)
                val res = ServiceResultData(
                    code = member.code,
                    data = member.data,
                    message = member.message,
                )
                mSuccessLiveData.postValue(res)
            } catch (e: Exception) {
                mErrorLiveData.postValue("获取会员信息请求失败:${e.message}")
            }
        }
    }

    fun getMemberChange(newMembershipLevel:String,userId:Int){
        mViewModelScope.launch(mDispatcher){
            try {
                val member = apiService.getMemberChange(newMembershipLevel,userId)
                val res = ServiceResultData(
                    code = member.code,
                    data = member.data,
                    message = member.message,
                )
                mSuccessLiveData.postValue(res)
            } catch (e: Exception) {
                mErrorLiveData.postValue("会员失败:${e.message}")
            }
        }
    }
}